﻿using Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace DataAccess
{
    public class Common<T> where T:class, new ()
    {
        static T t;
        public bool Insert(T t, string Sp) 
        {
            try
            {
                var listpro = t.GetType().GetProperties();
                object[] q = new object[listpro.Length];

                for (int i = 0; i < listpro.Length; i++)
                {
                    q[i] = listpro[i].GetValue(t, null);
                }
                return Core.DataProvider.Instance.ExecuteNonQuery(Sp, q) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool UpDate(T t, string Sp) 
        {
            try
            {
                var listpro = t.GetType().GetProperties();
                object[] q = new object[listpro.Length];
                for (int i = 0; i < listpro.Length; i++)
                {
                    q[i] = listpro[i].GetValue(t, null);
                }
               return Core.DataProvider.Instance.ExecuteNonQuery(Sp, q) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(object b, string Sp)
        {
            try
            {
                return Core.DataProvider.Instance.ExecuteNonQuery(Sp, b) > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public IDataReader Select_ID(object b, string sp) 
        {
            try
            {
                return DataProvider.Instance.ExecuteReader(sp, b);
            }
            catch
            {
                return null;
            }
        }
        public IDataReader Select_IDnew(object[] b, string sp)
        {
            try
            {
                return DataProvider.Instance.ExecuteReader(sp, b);
            }
            catch
            {
                return null;
            }
        }
        public IDataReader Select_Name(object b, string sp) 
        {
            try
            {
                return DataProvider.Instance.ExecuteReader(sp,b);
            }
            catch
            {
                return null;
            }
        }

        public IDataReader Select_All(string sp)
        {
            return DataProvider.Instance.ExecuteReader(sp);
        }
         //
        public List<T> SelectAll<T>(string sp) where T : class, new()
        {
            try
            {
                return CBO.FillCollection<T>(DataProvider.Instance.ExecuteReader(sp));
            }
            catch (Exception)
            {
                return new List<T>();
            }
        }
    }
}
